package com.liuxy.androidutil;

import java.lang.reflect.Method;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;

public class StatusBarUtil {
	private static final String TAG = "MyTools.StatusBarUtil";
	private static Class<?> statusBarManager;
	private static Object sbService;
	
	private static void init(Context ctx){
		if (statusBarManager==null||sbService==null) {
			try {
				statusBarManager = Class.forName("android.app.StatusBarManager");
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
			sbService=ctx.getApplicationContext().getSystemService("statusbar");
			//statusBarManager=sbService.getClass();
		}
	} 
	
	@TargetApi(Build.VERSION_CODES.DONUT)
	public static final void collapseStatusBar(Context ctx) {
		init(ctx);
		try {
			Method collapse;
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
				collapse = statusBarManager.getMethod("collapsePanels");
			} else {
				collapse = statusBarManager.getMethod("collapse");
			}
			collapse.invoke(sbService);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@TargetApi(Build.VERSION_CODES.DONUT)
	public static final void expandStatusBar(Context ctx) {
		init(ctx);
		try {
			Method expand;
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
				expand = statusBarManager.getMethod("expandNotificationsPanel");
			} else {
				expand = statusBarManager.getMethod("expand");
			}
			expand.invoke(sbService);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private StatusBarUtil() {
		/* cannot be instantiated */
		throw new UnsupportedOperationException(TAG + " cannot be instantiated");
	}
}
